package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.ILocalQuerySceneOrderAttrService;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.OrderSummaryInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.service.batch.BatchAcceptUtil;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * 订单概要信息
 */
@Service
public class OrderSummaryInfoModuleHandler extends AbstractModuleHandler {

    @Autowired
    ILocalQuerySceneOrderAttrService querySceneOrderAttrService;

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        OrderSummaryInfoVO orderSummaryVO = new OrderSummaryInfoVO();
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        List<KiteAttr> commonAttrList = querySceneInstService.listCommonAttrsByModuleId(sceneInstId, "140");
        //把业务号码属性补充进来展示
        // 批量起租不展示业务号码
        if (!BatchAcceptUtil.isBatchStartRent(sceneInstId)) {
            KiteAttr accNbrAttr = querySceneInstService.getCommonAttr(sceneInstId, "business_number");
            if (Objects.nonNull(accNbrAttr)) {
                commonAttrList.add(accNbrAttr);
            }
        }
        else {
            Map<String, Object> orderInfo = querySceneInstService.getExtParameterValue(sceneInstId, "batchOrderInfo");
            String rootOrderId = KiteMapUtils.getString(orderInfo, "rootOrderId");
            commonAttrList.forEach(commonAttr -> {
                if (KiteStringUtils.equals("ORD10197", commonAttr.getFieldName())) {
                    commonAttr.setValue(rootOrderId);
                    commonAttr.setValueDesc(rootOrderId);
                }
            });
        }

        if (KiteListUtils.isNotEmpty(commonAttrList)) {
            List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(commonAttrList);
            // 信息返回空，不展示
            if (KiteListUtils.isEmpty(instAttrDTOList)) {
                return Lists.newArrayList();
            }
            orderSummaryVO.setOrderSummaryAttr(instAttrDTOList);
            SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
            sceneAccountInfoVo.setOrderSummaryInfo(orderSummaryVO);
        }
        return Collections.singletonList(orderSummaryVO);
    }

    @Override
    public List<ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        OrderSummaryInfoVO orderSummaryVO = new OrderSummaryInfoVO();
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        List<KiteAttr> commonAttrList = querySceneInstService.listCommonAttrsByModuleId(sceneInstId, "140");
        //把业务号码属性补充进来展示
        KiteAttr accNbrAttr = querySceneInstService.getCommonAttrsByFieldName(sceneInstId, "business_number", "113");
        if (Objects.nonNull(accNbrAttr)) {
            commonAttrList.add(accNbrAttr);
        }
        if (KiteListUtils.isNotEmpty(commonAttrList)) {
            //设置预览模式
            commonAttrList.forEach((attr) -> {
                attr.setAttrValueTypeId("TT");
            });
            List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(commonAttrList);
            // 信息返回空，不展示
            if (KiteListUtils.isEmpty(instAttrDTOList)) {
                return Lists.newArrayList();
            }
            orderSummaryVO.setOrderSummaryAttr(instAttrDTOList);
            SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
            sceneAccountInfoVo.setOrderSummaryInfo(orderSummaryVO);
        }
        return Collections.singletonList(orderSummaryVO);
    }

}
